package com.woniuxy.dudugua.util.aifaceutil;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class AIFace {
    public String Start(String image) throws Exception {
        /**
         * 开通选项：
         * https://api.yimei.ai/apimgr/static/help.html
         *开通的功能
         * - 击败人数 defeat_rank    137438953472
         * - 肌肤年龄 age            1
         * - 肤质 skin_type      8
         * - 黑头 blackhead      32768
         * - 皱纹 wrinkle        8192
         * - 毛孔 pore pore           4
         * - 黑眼圈 dark_circle    131072
         * - 痘痘 pockmark       65536
         * - 颜值 appearance     262144
         * - 水分 moisture	      4096
         */
        String url = "https://api.yimei.ai/v2/api/face/analysis/137439457293";
        String client_id = "c99be59f3d669b79";
        String client_secret = "61e0be2874dd80cd19cbcf7ca863c007";
        String body = "image=" + image;
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpURLConnection.setRequestProperty("Authorization", "Basic " + new sun.misc.BASE64Encoder().encode((client_id + ":" + client_secret).getBytes()));
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(body.length()));

        OutputStream outputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;
        try {
            outputStream = httpURLConnection.getOutputStream();
            outputStreamWriter = new OutputStreamWriter(outputStream);
            outputStreamWriter.write(body);
            outputStreamWriter.flush();
            //响应失败
            if (httpURLConnection.getResponseCode() != 200) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            //接收响应流
            inputStream = httpURLConnection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            while ((tempLine = reader.readLine()) != null) {
                resultBuffer.append(tempLine);
            }
        } finally {
            if (outputStreamWriter != null) outputStreamWriter.close();
            if (outputStream != null) outputStream.close();
            if (reader != null) reader.close();
            if (inputStreamReader != null) inputStreamReader.close();
            if (inputStream != null) inputStream.close();
        }
        return resultBuffer.toString();
    }
}
